﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 
    /// </summary>
    public class DataTableColumnService : CrudAppService<DataTableColumn, DataTableColumnDto, Guid, DataTableColumnInput, DataTableColumnCreateDto, DataTableColumnUpdateDto>
    {
        private IRepository<DataTableColumn, Guid> _stConfigRepository;

        public DataTableColumnService(IRepository<DataTableColumn, Guid> StConfigRepository) : base(StConfigRepository)
        {
            _stConfigRepository = StConfigRepository;
        }

        /// <summary>
        /// 获取列单词
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DataTableColumnDto>> GetListAsync(DataTableColumnInput input)
        {
            var iQuery = await _stConfigRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.HeaderName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DataTableColumn>, List<DataTableColumnDto>>(iQuery.ToList());

                return new PagedResultDto<DataTableColumnDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DataTableColumn>, List<DataTableColumnDto>>(items.ToList());

                return new PagedResultDto<DataTableColumnDto>(totalCount, itemsDto);
            }
        }
    }
}
